/*
 * 作者: 侯晓宇
 * 时间: 2017-04-05
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	require("jquery");

	var c = require('app/utils/CommonUtils');
	var tip = require('app/utils/TipUtils');
	var hc = require('app/utils/HttpClientUtils');
	var appInfo = require('app/AppInfo');

	var action = require('./action/AdminUserFrmAction');
	var dialog = new auiDialog({});
	
	var param = api.pageParam;
	var keyword = param.text;

	var usertype = "";
	if(param.index == 2){
		$("#addAdmin").hide();
		usertype = "1";
	}else if(param.index == 3){
		$("#addAdmin").show();
		usertype = "9";
	}else{
		$("#addAdmin").hide();
	}
	
    function promptTel(func_callback){
    	dialog.prompt({
            title:"添加管理员",
            text:'请输入管理员手机号',
            buttons:['取消','确定']
        },function(ret){
        	func_callback(ret);
        })
    }
    
    function promptTel_callback(ret){
    	if(ret.buttonIndex == 2){
            var tel = ret.text;
            
            if(c.isTel(tel)){
            	
				var url = appInfo.serverbase + "/luyan/admin/user/add_admin";
            	var body = {};
            	body.tel = tel;
            	hc.post(url,body,function(resp){
            		if(resp.status == "200"){
	            		if(resp.result){
	            			location.reload();
	            		}else{
	            			tip.show("未通过该手机号找到用户");
	            		}
            		}
            		
            	});
            	
            }else{
            	tip.show("请正确输入手机号");
            	promptTel(promptTel_callback);
            }
            
        }else{
        	
        }
    		
    }
    $("#addAdmin").click(function(){
    	promptTel(promptTel_callback);
    });
    
	action.setUserType(usertype);
	
	//初始化搜索结果
	action.fetchInHeaderMode();
	//初始化头部刷新
	api.setRefreshHeaderInfo({
		visible: true,
		loadingImg: 'widget://res/pullrefresh/pullpoint.png',
		bgColor: '#ccc',
		textColor: '#fff',
		textDown: '下拉刷新...',
		textUp: '松开刷新...',
		showTime: true
	}, function(ret, err) {
		action.fetchInHeaderMode();
	});

	//初始化底部刷新
	api.addEventListener({
		name: 'scrolltobottom',
		extra: {
			threshold: 500 //设置距离底部多少距离时触发，默认值为0，数字类型
		}
	}, function(ret, err) {
		action.fetchInFooterMode();
	});
	
	

	return;
});